package com.cz.fra.security.impl;

import com.cz.fra.dao.security.JdbcSecurityDao;
import com.cz.fra.dao.security.SecurityDao;
import com.cz.fra.entity.User;
import com.cz.fra.security.AuthorizationException;
import com.cz.fra.security.SecurityService;

public class SecurityServiceImpl implements SecurityService{

	SecurityDao securityDao = new JdbcSecurityDao();
	
	@Override
	public User getUser(String username, String password)
			throws AuthorizationException {
		User user = null;
		
		try {
			if (isValidUserName(username) && isValidUserPassword(password)) 
			{
				user = this.securityDao.getUserData(username, password);
			}
		} catch (Exception ex) {
			throw new AuthorizationException("Couldn't authorize user");
		}
		return user;
	}

	/**
	 * Validate provided UserName
	 * 
	 * @return boolean
	 */
	public boolean isValidUserName(String userName) {
		boolean isValid = false;

		if (userName != null && !userName.trim().equals("")) {
			isValid = true;
		}
		return isValid;
	}

	/**
	 * Validate provided Password
	 * 
	 * @return boolean
	 */
	public boolean isValidUserPassword(String userPassword) {
		boolean isValid = false;

		if (userPassword != null && !userPassword.trim().equals("")) {
			isValid = true;
		}
		return isValid;
	}
}
